
#######################################
####  Crossnational Survey Data   #####
#######################################

# This R-Script allows to replicate Figure S1-1 and Figure SI-2 in the appendix. 
# The analysis was performed on R Version 4.1.2
# Clear environment #############

rm(list = ls())
gc()

# Installing and Loading Packages #############
#install.packages("dplyr")
#install.packages("srvyr")
#install.packages("ggplot2")
#install.packages("haven")
#install.packages("forcats")

library(dplyr)
library(srvyr)
library(ggplot2)
library(haven)
library(forcats)

# Set Up Workspace and Load Datasets ######
# Insert the appropriate directory path in place of 'insert here..'.
setwd("insert here.. /Replication Materials - PSRM")

# Define the directory where figures will be saved
plotpath<-"insert here.. /Replication Materials - PSRM/Figures/"


# ISSP 2016 Dataset -----
# Download "ZA6900_v2-0-0.dta" from https://search.gesis.org/research_data/ZA6900
# Alternatively, use the data file "ISSP_2016_data.RDS" with the selected variables.

# Load the ISSP 2016 dataset
ISSP_data <- readRDS("...insert your directory/Replication Materials - PSRM 2/Data/Crossnational surveys/ISSP_2016_data.RDS")


## ESS 2012-2018 Survey   -----
# Download the ESS6-9e01_1 from: https://ess-search.nsd.no/CDW/ConceptVariables or use the data file "ESS_20162018_data.RDS" with the selected variables
# Alternatively, use the dataset "ESS_20162018_data.RDS" with the selected variables.
ESS_data <- readRDS("...insert your directory.../Replication Materials - PSRM 2/Data/Crossnational surveys/ESS_20162018_data.RDS")

# Figure (SI-1) Government Responsibility to Reduce Income Differences, by Country ######
ISSP_data<-ISSP_data %>%
  mutate(country2=case_when(country==826~"United Kingdom", 
                            country==36~"Australia", 
                            country==56  ~"Belgium",
                            country==152~"Chile",
                            country==158~"Taiwan",
                            country==191~"Croatia",
                            country==203~"Czech Republic", 
                            country==208~"Denmark",
                            country==246~"Finland",
                            country==250~"France",
                            country==268~"Georgia", 
                            country==276~"Germany", 
                            country==348~"Hungary",  
                            country==352~"Iceland",  
                            country==356~"India",
                            country==376~"Israel", 
                            country==410~"Korea (South)", 
                            country==428~"Latvia",
                            country==440~"Lithuania", 
                            country==392~"Japan", 
                            country==554~"New Zealand", 
                            country==578~"Norway", 
                            country==608~"Philippines", 
                            country==643~"Russia",
                            country==703~"Slovakia", 
                            country==705~"Slovenia",
                            country==710~"South Africa", 
                            country==752~"Sweden",
                            country==724~"Spain", 
                            country==740~"Suriname", 
                            country==756~"Switzerland", 
                            country==764~"Thailand", 
                            country==792~"Turkey", 
                            country==840~"United States", 
                            country==862~"Venezuela" )) %>%
  filter(!(country2 %in% c("Chile", "Taiwan", "Georgia",  "Lithuania", "Croatia","Georgia",
                           "Philippines", "India", "South Africa", "Suriname", 
                           "Thailand", "Venezuela")))

# Generate new variable based on the Main outcome quesiton, takes the value of “1” if respondents answered in the aﬃrmative (either “Deﬁinitely should be,” or “Probably should be”), and "0, if otherwise.
# v27 - Q7g Government responsibility: reduce income differences rich/ poor: "On the whole, do you think it should or should not be the government's responsibility to reduce income differences between the rich and the poor.

ISSP_data<-
  ISSP_data %>%
  mutate(MainQ_support= case_when(v27==1 | v27==2~1,
                                  v27==3 | v27==4~0))

# Generate summary statistics
Fig_SI1_data <- ISSP_data %>%
  dplyr::filter(!is.na(MainQ_support)) %>%
  as_survey_design(weights = c(WEIGHT)) %>%
  dplyr::group_by(country2) %>%
  dplyr::summarize(n = survey_mean(MainQ_support, na.rm = FALSE, vartype = "ci")) %>%
  mutate(prop = n*100) %>%
  mutate(n_lowprop = n_low*100) %>%
  mutate(n_uppprop = n_upp*100) 

Fig_SI1<-
  Fig_SI1_data %>%
  mutate(country2 = fct_reorder(country2, desc(prop))) %>%
  ggplot()+ aes(x=country2,y=prop) + 
  geom_hline(yintercept = 50, colour="#A30D3C", size=0.9, alpha=0.8)+ coord_flip()+
  geom_linerange(aes(ymin=0,ymax=prop), colour="#8A8A8A")+
  geom_point(size=2.7, colour="#1E7875")+
  scale_y_continuous(labels = function(x) paste0(x, "%"))+
  labs(x = " ", y = "", title="") +
  theme_minimal() +
  theme(legend.position = "none",
        axis.text=element_text(size=13))
theme(legend.position = "none")
Fig_SI1

ggsave(Fig_SI1, dpi = 400,
       filename = paste0(plotpath, "Fig_SI1.png"),
       height = 6, width = 10, device = "png")
setwd(plotpath)
system("open Fig_SI1.png")



# Figure (SI-2) Average Support for Abstract versus Concrete Social Policy ######
ISSP_data$v23[ISSP_data$v23 > 4]  <- NA
ISSP_data$v26[ISSP_data$v26 > 4]  <- NA
ISSP_data$v28[ISSP_data$v28 > 4]  <- NA
ISSP_data$v29[ISSP_data$v29 > 4]  <- NA
ISSP_data$v27[ISSP_data$v27 > 4]  <- NA
ISSP_data<-ISSP_data %>% mutate(v23_rev=5-v23)
ISSP_data<-ISSP_data %>% mutate(v26_rev=5-v26)
ISSP_data<-ISSP_data %>% mutate(v28_rev=5-v28)
ISSP_data<-ISSP_data %>% mutate(v29_rev=5-v29)
ISSP_data<-ISSP_data %>% mutate(v27_rev=5-v27)

ISSP_data$welfare_concrete <- rowMeans(cbind(ISSP_data$v23_rev, 
                                             ISSP_data$v26_rev, 
                                             ISSP_data$v28_rev, 
                                             ISSP_data$v29_rev), na.rm=T)

ISSP_support_welfare_concrete<-
  ISSP_data %>%
  filter(!is.na(welfare_concrete)) %>%
  group_by(country2) %>%
  summarize(mean=mean(welfare_concrete)) 

ISSP_support_abstact<-
  ISSP_data %>%
  filter(!is.na(v27_rev)) %>%
  group_by(country2) %>%
  summarize(mean=mean(v27_rev))

FigSI2_data <- bind_rows(ISSP_support_abstact,
                         ISSP_support_welfare_concrete,
                         .id="Question")

FigSI2_data$Question<-factor(FigSI2_data$Question,
                             levels=c(1,2),
                             labels = c("Abstract measures about reducing inequality",
                                        "Concrete measures about reducing inequality"))

Fig_SI2<-
  FigSI2_data %>%
  group_by(Question) %>%
  mutate(country2 = fct_reorder(country2, desc(mean))) %>%
  ggplot()+ aes(x=country2,y=mean) + 
  coord_flip()+
  geom_line( colour="#8A8A8A")+
  geom_point(aes(color=Question), size=2)+
  scale_fill_manual(values = c("#CCAB52", "#1E7875" ))+
  scale_colour_manual(values = c("#CCAB52", "#1E7875"))+
  theme_minimal() +
  labs(x = "", y = "", title="", colour="", fill="", shape="") +
  theme( legend.position = "bottom", legend.text = element_text(size = 13),
         legend.key.size = unit(0.8, "cm"), legend.direction='vertical',
         axis.text = element_text(size = 13))
Fig_SI2
ggsave(Fig_SI2, dpi = 500,
       filename = paste0(plotpath, "Fig_SI2.png"),
       height = 7, width = 10, device = "png")
setwd(plotpath)
system("open Fig_SI2.png")


# Figure (SI-3) Government Responsibility to Reduce Income Diﬀerences, by Country and Year #######

ESS_data$fullweight = ESS_data$pspwght * ESS_data$pweight
ESS_data<-ESS_data%>%
  mutate(cntry2=case_when(cntry=="AT"~"Austria",
                          cntry=="BE"~"Belgium",
                          cntry=="BG"~"Bulgaria",
                          cntry=="CH"~"Switzerland",
                          cntry=="CY"~"Cyprus",
                          cntry=="CZ"~"Czechia",
                          cntry=="DE"~"Germany",
                          cntry=="DK"~"Denmark",
                          cntry=="EE"~"Estonia",
                          cntry=="ES"~"Spain",
                          cntry=="FI"~"Finland",
                          cntry=="FR"~"France",
                          cntry=="GB"~"United Kingdom",
                          cntry=="GR"~"Greece",
                          cntry=="HR"~"Croatia",
                          cntry=="HU"~"Hungary",
                          cntry=="IE"~"Ireland",
                          cntry=="IL"~"Israel",
                          cntry=="IS"~"Iceland",
                          cntry=="IT"~"Italy",
                          cntry=="LT"~"Lithuania",
                          cntry=="LU"~"Luxembourg",
                          cntry=="LV"~"Latvia",
                          cntry=="NL"~"Netherlands",
                          cntry=="NO"~"Norway",
                          cntry=="PL"~"Poland",
                          cntry=="PT"~"Portugal",
                          cntry=="RU"~"Russia",
                          cntry=="SE"~"Sweden",
                          cntry=="SI"~"Slovenia",
                          cntry=="SK"~"Slovakia",
                          cntry=="TR"~"Turkey",
                          cntry=="UA"~"Ukraine"
  ))
ESS_data$Wave<-factor(ESS_data$essround, levels = c(6,7,8,9),
                      labels = c("2012", "2014", "2016", "2018"))

# gincdif: Government should reduce differences in income levels: Using this card, please say to what extent you agree or disagree with each of the following statements. The government should take measures to reduce differences in income levels
# 1	Agree strongly
# 2	Agree
# 3	Neither agree nor disagree
# 4	Disagree
# 5	Disagree strongly

ESS_data<-
  ESS_data %>%
  mutate(mainQ_num= case_when(gincdif==1 | gincdif==2 ~1,
                              gincdif==3 | gincdif==4 | gincdif==5 ~0))

FigSI3_data <- ESS_data %>%
  dplyr::filter(!is.na(mainQ_num)) %>%
  as_survey_design(weights = c(fullweight)) %>%
  group_by(cntry2, Wave) %>%
  dplyr::summarize(n = survey_mean(mainQ_num, na.rm = FALSE, vartype = "ci")) %>%
  mutate(prop = n*100) %>%
  ungroup(cntry2,Wave)

FigSI3_data$cntry2<-factor(FigSI3_data$cntry2)

Fig_SI3<-
  FigSI3_data %>%
  mutate(cntry2 = fct_reorder(cntry2, desc(prop))) %>%
  ggplot()+ aes(x=cntry2,y=prop) + 
  geom_hline(yintercept = 50, colour="#A30D3C", size=0.6, alpha=0.6)+
  coord_flip()+
  geom_linerange(aes(ymin=0,ymax=prop), colour="#D9D9D9", size=0.6)+
  geom_point(aes(fill=Wave, colour=Wave, shape=Wave), size=2.5)+
  scale_y_continuous(labels = function(x) paste0(x, "%"), limits = c(0,100))+
  labs(x = " ", y = "", title="") + 
  #facet_wrap(facets=~Wave,nrow=2)+
  scale_fill_manual(values = c("#CCAB52", "#96B0A6", "#196266", "#1C484A"))+   
  scale_colour_manual(values  =c("#CCAB52", "#96B0A6", "#196266", "#1C484A"))+   
  theme_minimal() +
  theme(legend.position = "bottom",
        axis.text=element_text(size=13))
Fig_SI3


ggsave(Fig_SI3, dpi = 400,
       filename = paste0(plotpath, "Fig_SI3.png"),
       height = 5, width = 10, device = "png")
setwd(plotpath)
system("open Fig_SI3.png")
